from django.db import models
from django.core.cache import cache
# Create your models here.


class Category(models.Model):
    name = models.CharField(max_length=100, verbose_name='分类名称')
    description = models.TextField(verbose_name='分类描述', null=True, blank=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')


    class Meta:
        db_table = 'edu_category'
        verbose_name = '分类'
        verbose_name_plural = verbose_name


class Book(models.Model):
    name = models.CharField(max_length=100, verbose_name='图书名称')
    writer = models.CharField(max_length=100, verbose_name='作者')
    ISBN = models.CharField(max_length=100, verbose_name='ISBN')
    category = models.ForeignKey(
        Category,
        verbose_name='分类',
        related_name='books',
        on_delete=models.CASCADE
    )
    STAT = (
        (1, '可借'),
        (2, '不可借'),
    )
    status = models.IntegerField(choices=STAT, default=1, verbose_name='状态')
    description = models.TextField(verbose_name='图书描述', null=True, blank=True)
    is_del = models.BooleanField(default=False, verbose_name='是否删除')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='借出时间')


    class Meta:
        db_table = 'edu_book'
        verbose_name = '图书'
        verbose_name_plural = verbose_name



    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        cache.delete('book:list')

    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)
        cache.delete('book:list')